package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.util.FormulaUtil
import com.yc.yclibrary.base.YcAppCompatActivity
import kotlinx.android.synthetic.main.actionbar_layout.*
import kotlinx.android.synthetic.main.type2_formula4_location_activity.*
import kotlinx.android.synthetic.main.formula_button.*
import kotlinx.android.synthetic.main.type3_formula1_location_activity.*
import kotlinx.android.synthetic.main.type3_formula2_location_activity.*
import kotlinx.android.synthetic.main.type3_formula3_location_activity.*
import kotlinx.android.synthetic.main.type3_formula4_location_activity.*
import java.lang.NumberFormatException

/**
 *  电子学 - 已知功放输入、输出电压，求功放的放大倍数、增益
 */
class Type3Formula4Activity : BaseActivity() {

    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, Type3Formula4Activity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.type3_formula4_location_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        formulaResetBtn.setOnClickListener {
            formula3_4_V1_TEV.reset()
            formula3_4_W1_TEV.reset()
            formula3_4_X1_TEV.reset()
            formula3_4_dB1_TEV.reset()
            formula3_4_V2_TEV.reset()
            formula3_4_W2_TEV.reset()
            formula3_4_X2_TEV.reset()
            formula3_4_dB2_TEV.reset()
        }
        formulaOkBtn.setOnClickListener {
            try {
                val result8X = FormulaUtil.formula3_4_1(
                    formula3_4_V1_TEV.getDouble(),
                    formula3_4_W1_TEV.getDouble(),
                    8
                )
                val result8dB = FormulaUtil.formula3_4_2(
                    formula3_4_V1_TEV.getDouble(),
                    formula3_4_W1_TEV.getDouble(),
                    8
                )
                val result4X = FormulaUtil.formula3_4_1(
                    formula3_4_V2_TEV.getDouble(),
                    formula3_4_W2_TEV.getDouble(),
                    4
                )
                val result4dB = FormulaUtil.formula3_4_2(
                    formula3_4_V2_TEV.getDouble(),
                    formula3_4_W2_TEV.getDouble(),
                    4
                )
                formula3_4_X1_TEV.setText(result8X)
                formula3_4_dB1_TEV.setText(result8dB)
                formula3_4_X2_TEV.setText(result4X)
                formula3_4_dB2_TEV.setText(result4dB)
            } catch (e: NumberFormatException) {
                formula3_4_X1_TEV.setText("Infinity")
                formula3_4_dB1_TEV.setText("Infinity")
                formula3_4_X2_TEV.setText("Infinity")
                formula3_4_dB2_TEV.setText("Infinity")
            }
        }
    }
}